#!/bin/bash

# OE构建nodejs工程文档
# @doc  http://wiki.intra.xiaojukeji.com/pages/viewpage.action?pageId=114610181



######################## [配置项] ####################################
#
# 每个集群对应的build参数
declare -A ODIN_CLUSTER_CONF
ODIN_CLUSTER_CONF=(
  # ["prod"]=":prod"
#   ["hna-pre-v"]=":cn-pre"                # 弹性云集群 - 国内机房-pre
  ["hnc-v"]=":cn"                      # local dev environment
  #["us01-pre-v"]=":us-pre"                   # 弹性云集群 - 美东机房-pre
  ["us01-v"]=":us"                   # 弹性云集群 - 美东机房
  # ["de01-pre-v"]=":de-pre"                   # 弹性云集群 - 欧洲机房-pre
  # ["de01-v"]=":de"                   # 弹性云集群 - 欧洲机房
)

# 每个集群对应的域名
declare -A ODIN_CLUSTER_DOMAIN
ODIN_CLUSTER_DOMAIN=(
  ["hnc-v"]="assess.intra.xiaojukeji.com"              # local dev environment
  ["us01-v"]="isi.intra.xiaojukeji.com"                      # local dev environment
)


######################## [环境准备] ####################################
#
rm -rf output
mkdir output

readonly DOCKER=./docker

cp $DOCKER/control.sh output/  # 拷贝control.sh脚本 至output目录下
# cp $DOCKER/control.*.sh output/  # 拷贝control.sh脚本 至output目录下
chmod 744 output/control.sh
#
# 用户自己的Dockerfile需要放在代码git库中，并确保OE／scmpf 构建后的输出目录里， Dockerfile在根目录位置；
# Odin部署上线时， 如果发现根目录位置有Dockerfile文件，会优先使用这个Dockerfile进行镜像编译，然后进行部署。
#
cp $DOCKER/Dockerfile output/
cp -r $DOCKER/init output/



######################## [server.[].conf创建] ####################################
#
readonly CONF_FILE=$DOCKER/server.conf

for key in ${!ODIN_CLUSTER_DOMAIN[@]};
do
  site=$key
  DOMAIN=${ODIN_CLUSTER_DOMAIN[$key]}
  echo "======= [info] current site: $site using domain: $DOMAIN ======="

  SITE_CONF_FILE=$DOCKER/server.$site.conf

  # 创建server.[].conf配置文件， eg: server.hnb-pre-v.conf
  cp $CONF_FILE $SITE_CONF_FILE
  # 执行字符替换，eg: sed -i.back "s#@DOMAIN@#intra.xiaoju#g" tmp.server.conf
  #备份加替换
  sed -i.back "s#@DOMAIN@#$DOMAIN#g" $SITE_CONF_FILE

  # 替换root 路径
  ROOT_PATH=/home/xiaoju/sec-evaluation-portal/$site
  sed -i.back "s#@ROOT_PATH@#$ROOT_PATH#g" $SITE_CONF_FILE


  cp $SITE_CONF_FILE output/

  server_conf_ctx=`cat $SITE_CONF_FILE`
  echo -e "[server.conf] $SITE_CONF_FILE\n$server_conf_ctx\n"
  # rm -rf $SITE_CONF_FILE $SITE_CONF_FILE.back

done



######################## [编译开始] ####################################
#
source ~/.nvm/nvm.sh
# nvm use v8.9.1
nvm use v12.20.0

#npm install -g yarn

# git pull
echo '======= [debug] install start ======='
npm install #安装依赖
echo '======= [debug] install end ======='


for key in ${!ODIN_CLUSTER_CONF[@]};
do
  site=$key
  ACTIVE=${ODIN_CLUSTER_CONF[$key]}
  echo "======= [debug] run -> npm run build$ACTIVE ======="
  # eg: npm run build:cn-pre
  npm run build$ACTIVE
  echo "======= [debug] npm build [$site] end. [$ret]======="

  ret=$?

  if [ $ret -ne 0 ];then
      echo "===== build [$site] failure ====="
      exit $ret
  else
      echo -n "===== build [$site] successfully! ====="
  fi

  # tas-data-governance-portal 统一增加data前缀，对应的静态资源目录也统一增加
  SITE_PATH=output/$site
  mkdir -p $SITE_PATH

  mv dist/* $SITE_PATH  #拷贝dist目录下的所有文件至output/[站点] 目录下

done

######################## [编译结束] ####################################
# mv dist/* output/  #拷贝dist目录下的所有文件至output目录下


######################## [编译结果打印] ####################################
# 打印当前目录结构
# for dir in $(ls -al)
# do
#  [ -d $dir ] && echo "===== [debug] [build result] $dir"
# done

